namespace GemBox.Spreadsheet
{
    using System;
    using System.Collections;
    using System.Drawing;
    using System.Globalization;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Xml;

    internal class XlsxReadDirector : XlsxDirector
    {
        private ArrayList borders;
        private ArrayList cellStyles;
        private Color[] colors;
        private ArrayList fillPatterns;
        private ArrayList fonts;
        private NumberFormatCollection numberFormats;
        private bool preserveTemplate;
        private ArrayList savedDefinedNames;
        private ArrayList strings;

        public XlsxReadDirector(PackageBuilderBase builder, ExcelFile excelFile, bool preserveTemplate) : base(builder, excelFile)
        {
            this.strings = new ArrayList();
            this.numberFormats = new NumberFormatCollection(true);
            this.fonts = new ArrayList();
            this.fillPatterns = new ArrayList();
            this.borders = new ArrayList();
            this.cellStyles = new ArrayList();
            this.savedDefinedNames = new ArrayList();
            this.preserveTemplate = preserveTemplate;
            if (preserveTemplate)
            {
                XlsxExcelFilePreservationData data = new XlsxExcelFilePreservationData();
                data.WorkbookContentType = (string) base.builder.Parts["/xl/workbook.xml"];
                base.excelFile.Tag = data;
            }
        }

        private void AddBordersDataCreateNew(ref BordersData bordersData)
        {
            this.borders.Add(bordersData);
            bordersData = new BordersData();
        }

        private void AddFontDataCreateNew(ref ExcelFontData fontData)
        {
            if (fontData.Size == -1)
            {
                fontData.Size = base.excelFile.DefaultFontSize;
            }
            if (fontData.Name == null)
            {
                fontData.Name = base.excelFile.DefaultFontName;
            }
            if (this.fonts.Count == 0)
            {
                base.excelFile.DefaultFontName = fontData.Name;
                base.excelFile.DefaultFontSize = fontData.Size;
            }
            this.fonts.Add(fontData);
            fontData = new ExcelFontData(null, -1);
        }

        private void CleanAllIndexes()
        {
            foreach (CellStyle style in this.cellStyles)
            {
                style.Element.Indexes = null;
            }
        }

        private Color ColorIndexToColor(int colorIndex)
        {
            if (colorIndex > (this.colors.Length - 1))
            {
                colorIndex = 0;
            }
            return this.colors[colorIndex];
        }

        public override void Construct()
        {
            this.ReadSharedStrings();
            this.ReadStyles();
            this.ReadWorksheets();
            this.CleanAllIndexes();
            base.excelFile.CellStyleCache.EmptyAddQueue();
        }

        private void ConvertColorIndexIfNeeded(ref int colorIndex, ref Color color, Color defaultColor)
        {
            if (colorIndex != -1)
            {
                if (color.ToArgb() == defaultColor.ToArgb())
                {
                    color = this.ColorIndexToColor(colorIndex);
                }
                colorIndex = -1;
            }
        }

        private void ConvertIndexes()
        {
            foreach (ExcelFontData data in this.fonts)
            {
                this.ConvertColorIndexIfNeeded(ref data.ColorIndex, ref data.Color, Color.Black);
            }
            foreach (FillPatternData data2 in this.fillPatterns)
            {
                this.ConvertColorIndexIfNeeded(ref data2.PatternForegroundColorIndex, ref data2.PatternForegroundColor, Color.Black);
                this.ConvertColorIndexIfNeeded(ref data2.PatternBackgroundColorIndex, ref data2.PatternBackgroundColor, Color.White);
            }
            foreach (BordersData data3 in this.borders)
            {
                for (int i = 0; i < 5; i++)
                {
                    this.ConvertColorIndexIfNeeded(ref data3.BorderColorIndex[i], ref data3.BorderColor[i], Color.Black);
                }
            }
            foreach (CellStyle style in this.cellStyles)
            {
                CellStyleData element = style.Element;
                CellStyleDataIndexes indexes = element.Indexes;
                if (indexes.NumberFormatIndex > -1)
                {
                    element.NumberFormat = (string) this.numberFormats[indexes.NumberFormatIndex];
                }
                if (indexes.FontIndex > -1)
                {
                    element.FontData = (ExcelFontData) this.fonts[indexes.FontIndex];
                }
                if (indexes.FillPatternIndex > -1)
                {
                    FillPatternData data5 = (FillPatternData) this.fillPatterns[indexes.FillPatternIndex];
                    element.PatternStyle = data5.PatternStyle;
                    element.PatternForegroundColor = data5.PatternForegroundColor;
                    element.PatternBackgroundColor = data5.PatternBackgroundColor;
                }
                if (indexes.BordersIndex > -1)
                {
                    BordersData data6 = (BordersData) this.borders[indexes.BordersIndex];
                    element.BorderStyle = data6.BorderStyle;
                    element.BorderColor = data6.BorderColor;
                    element.BordersUsed = data6.BordersUsed;
                }
            }
        }

        private void GetFirstVisibleCellIndexes(ExcelWorksheet ws, XmlTextReader reader)
        {
            string attribute = reader.GetAttribute("topLeftCell");
            if (attribute != null)
            {
                int num;
                int num2;
                CellRange.PositionToRowColumn(attribute, out num, out num2);
                ws.ViewOptions.FirstVisibleRow = num;
                ws.ViewOptions.FirstVisibleColumn = num2;
            }
        }

        private static bool GetPortraitValue(string attrStr, bool defaultValue)
        {
            if (attrStr != null)
            {
                if (attrStr == "portrait")
                {
                    return true;
                }
                if (attrStr == "landscape")
                {
                    return false;
                }
            }
            return defaultValue;
        }

        private static bool GetPrintCellNotesValue(string attrStr, bool defaultValue)
        {
            if ((attrStr == null) || (!(attrStr == "asDisplayed") && !(attrStr == "atEnd")))
            {
                return defaultValue;
            }
            return true;
        }

        private static bool GetPrintNotesSheetEndValue(string attrStr, bool defaultValue)
        {
            return (((attrStr != null) && (attrStr == "atEnd")) || defaultValue);
        }

        private int GetPrintResolutionFromString(string str, int defaultResolution)
        {
            if ((str == null) || (str.Length == 0))
            {
                return defaultResolution;
            }
            long num = uint.Parse(str);
            if (num > 0x7fffffffL)
            {
                return defaultResolution;
            }
            return (int) num;
        }

        private static Color GetRGBStringFromThemeIndex(int themeIndex)
        {
            string rgbStr = null;
            switch (themeIndex)
            {
                case 1:
                    return SystemColors.WindowText;

                case 2:
                    rgbStr = "EEECE1";
                    break;

                case 3:
                    rgbStr = "1F497D";
                    break;

                case 4:
                    rgbStr = "4F81BD";
                    break;

                case 5:
                    rgbStr = "C0504D";
                    break;

                case 6:
                    rgbStr = "9BBB59";
                    break;

                case 7:
                    rgbStr = "8064A2";
                    break;

                case 8:
                    rgbStr = "4BACC6";
                    break;

                case 9:
                    rgbStr = "F79646";
                    break;

                case 10:
                    rgbStr = "0000FF";
                    break;

                case 11:
                    rgbStr = "800080";
                    break;

                default:
                    return SystemColors.Window;
            }
            return RgbStringToColor(rgbStr);
        }

        private static bool GetShowInPageBreakPreviewValue(string attrStr, bool defaultValue)
        {
            return (((attrStr != null) && (attrStr == "pageBreakPreview")) || defaultValue);
        }

        private AnchorCell ReadAnchorCell(ExcelWorksheet ws, XmlTextReader reader, string anchorCellName)
        {
            int num = 0;
            int num2 = 0;
            long columnOffset = 0L;
            if (reader.IsEmptyElement)
            {
                return null;
            }
            reader.Read();
            if (reader.NodeType != XmlNodeType.Element)
            {
                return null;
            }
            num = int.Parse(reader.ReadElementString("xdr:col"));
            columnOffset = long.Parse(reader.ReadElementString("xdr:colOff"));
            num2 = int.Parse(reader.ReadElementString("xdr:row"));
            return new AnchorCell(ws.Columns[num], ws.Rows[num2], columnOffset, long.Parse(reader.ReadElementString("xdr:rowOff")));
        }

        private static void ReadBorder(XmlTextReader reader, BordersData bordersData, int currentBorder)
        {
            string attribute = reader.GetAttribute("style");
            if ((attribute != null) && (attribute != "none"))
            {
                bordersData.BordersUsed |= CellBorder.MultipleFromIndividualBorder((IndividualBorder) currentBorder);
                bordersData.BorderStyle[currentBorder] = (LineStyle) Enum.Parse(typeof(LineStyle), attribute, true);
            }
        }

        private void ReadBorders(XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                BordersData bordersData = new BordersData();
                int currentBorder = -1;
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "border":
                            {
                                if (reader.GetAttribute("diagonalUp") == "1")
                                {
                                    bordersData.BordersUsed |= MultipleBorders.DiagonalUp;
                                }
                                if (reader.GetAttribute("diagonalDown") == "1")
                                {
                                    bordersData.BordersUsed |= MultipleBorders.DiagonalDown;
                                }
                                if (reader.IsEmptyElement)
                                {
                                    this.AddBordersDataCreateNew(ref bordersData);
                                }
                                continue;
                            }
                            case "left":
                            {
                                currentBorder = 2;
                                ReadBorder(reader, bordersData, currentBorder);
                                continue;
                            }
                            case "right":
                            {
                                currentBorder = 3;
                                ReadBorder(reader, bordersData, currentBorder);
                                continue;
                            }
                            case "top":
                            {
                                currentBorder = 0;
                                ReadBorder(reader, bordersData, currentBorder);
                                continue;
                            }
                            case "bottom":
                            {
                                currentBorder = 1;
                                ReadBorder(reader, bordersData, currentBorder);
                                continue;
                            }
                            case "diagonal":
                            {
                                currentBorder = 4;
                                string attribute = reader.GetAttribute("style");
                                if ((attribute != null) && (attribute != "none"))
                                {
                                    bordersData.BorderStyle[currentBorder] = (LineStyle) Enum.Parse(typeof(LineStyle), attribute, true);
                                }
                                continue;
                            }
                            case "color":
                            {
                                ReadColorElement(reader, ref bordersData.BorderColor[currentBorder], ref bordersData.BorderColorIndex[currentBorder]);
                                continue;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        switch (reader.Name)
                        {
                            case "left":
                            case "right":
                            case "top":
                            case "bottom":
                            case "diagonal":
                            {
                                currentBorder = -1;
                                continue;
                            }
                            case "border":
                            {
                                this.AddBordersDataCreateNew(ref bordersData);
                                continue;
                            }
                            case "borders":
                                return;
                        }
                    }
                }
            }
        }

        private void ReadCellXfs(XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                CellStyle style = null;
                bool applyAlignment = false;
                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        goto Label_0069;
                    }
                    string name = reader.Name;
                    if (name != null)
                    {
                        if (!(name == "xf"))
                        {
                            if (name == "alignment")
                            {
                                goto Label_0057;
                            }
                        }
                        else
                        {
                            style = this.ReadXfElement(reader, out applyAlignment);
                            this.cellStyles.Add(style);
                        }
                    }
                    continue;
                Label_0057:
                    if (applyAlignment)
                    {
                        this.ReadXfAlignment(reader, style.Element);
                    }
                    continue;
                Label_0069:
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "cellXfs"))
                    {
                        return;
                    }
                }
            }
        }

        private static void ReadColorElement(XmlReader reader, ref Color color, ref int colorIndex)
        {
            string attribute = reader.GetAttribute("rgb");
            if (attribute != null)
            {
                color = RgbStringToColor(attribute);
            }
            else
            {
                string s = reader.GetAttribute("indexed");
                if (s != null)
                {
                    colorIndex = int.Parse(s);
                }
                else
                {
                    int themeIndex = Utilities.GetOptAttrInt32(reader, "theme", -1);
                    if (themeIndex != -1)
                    {
                        color = GetRGBStringFromThemeIndex(themeIndex);
                    }
                }
            }
        }

        private void ReadColors(XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                int index = 0;
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "colors"))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "rgbColor"))
                    {
                        string attribute = reader.GetAttribute("rgb");
                        if (attribute != null)
                        {
                            this.colors[index] = RgbStringToColor(attribute);
                            index++;
                        }
                    }
                }
            }
        }

        private void ReadColumnInfos(ExcelWorksheet ws, XmlReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "cols"))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "col"))
                    {
                        int num4;
                        bool flag2;
                        bool flag3;
                        int num = Utilities.GetReqAttrInt32(reader, "min");
                        int num2 = Utilities.GetReqAttrInt32(reader, "max");
                        float num3 = Utilities.GetOptAttrFloat(reader, "width", 0f);
                        bool flag = Utilities.GetOptAttrBool01(reader, "customWidth", false);
                        ReadHiddenOutlineCollapsed(reader, out num4, out flag2, out flag3);
                        if ((flag || (num4 != -1)) || (flag2 || flag3))
                        {
                            for (int i = num - 1; i <= (num2 - 1); i++)
                            {
                                ExcelColumn excelColumnRow = ws.Columns[i];
                                if (flag)
                                {
                                    excelColumnRow.Width = (int) (num3 * 256f);
                                }
                                SetOutlineCollapsedHiddenIfNeeded(excelColumnRow, num4, flag2, flag3);
                            }
                        }
                    }
                }
            }
        }

        private Hashtable ReadCommentShapes(ExcelWorksheet ws, string legacyDrawingPath)
        {
            bool flag = false;
            bool flag2 = false;
            int num = 0;
            int num2 = 0;
            Hashtable hashtable = new Hashtable();
            Stream part = base.builder.GetPart(legacyDrawingPath);
            string[] strArray = null;
            XmlTextReader reader = new XmlTextReader(part);
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        if (reader.Name.Equals("v:shape"))
                        {
                            if (reader.GetAttribute("style").IndexOf("visibility:visible") != -1)
                            {
                                flag2 = true;
                            }
                        }
                        else if (reader.Name.Equals("x:ClientData"))
                        {
                            if (object.Equals(reader.GetAttribute("ObjectType"), "Note"))
                            {
                                flag = true;
                            }
                        }
                        else if (flag)
                        {
                            if (reader.Name.Equals("x:Row"))
                            {
                                num = Convert.ToInt32(reader.ReadElementString());
                                continue;
                            }
                            if (reader.Name.Equals("x:Column"))
                            {
                                num2 = Convert.ToInt32(reader.ReadElementString());
                            }
                            else if (reader.Name.Equals("x:Anchor"))
                            {
                                strArray = reader.ReadString().Trim().Split(new char[] { ',' });
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name.Equals("x:ClientData") && flag)
                        {
                            CommentShape shape = new CommentShape();
                            shape.IsVisible = flag2;
                            if (strArray != null)
                            {
                                shape.Left = int.Parse(strArray[0]);
                                shape.LeftOffset = int.Parse(strArray[1]);
                                shape.Top = int.Parse(strArray[2]);
                                shape.TopOffset = int.Parse(strArray[3]);
                                shape.Right = int.Parse(strArray[4]);
                                shape.RightOffset = int.Parse(strArray[5]);
                                shape.Bottom = int.Parse(strArray[6]);
                                shape.BottomOffset = int.Parse(strArray[7]);
                                shape.anchorSet = true;
                                strArray = null;
                            }
                            hashtable.Add(ws.Cells[num, num2], shape);
                            flag = false;
                            continue;
                        }
                        if (reader.Name.Equals("v:shape"))
                        {
                            flag2 = false;
                        }
                    }
                }
            }
            finally
            {
                reader.Close();
            }
            return hashtable;
        }

        private void ReadCommentsIfExists(ExcelWorksheet ws, Hashtable commentShapesVisible, string commentPath)
        {
            ArrayList list = new ArrayList();
            string position = null;
            string str2 = null;
            int num = 0;
            bool flag = false;
            if (commentPath != null)
            {
                XmlTextReader reader = new XmlTextReader(base.builder.GetPart("/xl/" + Path.GetFileName(commentPath)));
                try
                {
                    reader.MoveToContent();
                    while (reader.Read())
                    {
                        if (reader.NodeType == XmlNodeType.Element)
                        {
                            if (reader.Name == "author")
                            {
                                list.Add(reader.ReadString());
                            }
                            else if (reader.Name == "comment")
                            {
                                position = reader.GetAttribute("ref");
                                num = Utilities.GetReqAttrInt32(reader, "authorId");
                                flag = true;
                            }
                            else if ((reader.Name == "t") && flag)
                            {
                                if (str2 == null)
                                {
                                    str2 = reader.ReadString();
                                }
                                else
                                {
                                    str2 = str2 + reader.ReadString();
                                }
                            }
                        }
                        else if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "comment"))
                        {
                            int num2;
                            int num3;
                            ExcelCell cell = ws.Cells[position];
                            CellRange.PositionToRowColumn(position, out num2, out num3);
                            CommentShape shape = (CommentShape) commentShapesVisible[cell];
                            ExcelComment comment = cell.Comment;
                            CommentShape shape2 = comment.Shape;
                            comment.Author = list[num].ToString();
                            comment.Text = str2;
                            comment.IsVisible = shape.IsVisible;
                            shape2.Row = (ushort) num2;
                            shape2.Column = (ushort) num3;
                            if (shape.anchorSet)
                            {
                                shape2.Left = shape.Left;
                                shape2.LeftOffset = shape.LeftOffset;
                                shape2.Right = shape.Right;
                                shape2.RightOffset = shape.RightOffset;
                                shape2.Top = shape.Top;
                                shape2.TopOffset = shape.TopOffset;
                                shape2.Bottom = shape.Bottom;
                                shape2.BottomOffset = shape.BottomOffset;
                                shape2.anchorSet = true;
                            }
                            flag = false;
                            str2 = null;
                        }
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
        }

        private void ReadDefaultColumnWidth(ExcelWorksheet ws, XmlReader reader)
        {
            ws.DefaultColumnWidth = (int) (Utilities.GetOptAttrFloat(reader, "defaultColWidth", -1f) * 256f);
            if (ws.DefaultColumnWidth < 0)
            {
                ws.DefaultColumnWidth = 0x86e;
            }
        }

        private void ReadDrawings(ExcelWorksheet ws, string drawingXmlPath)
        {
            string partPath = Path.Combine("/xl/drawings/", Path.GetFileName(drawingXmlPath));
            SpreadsheetRelationshipCollection relationshipCollection = new SpreadsheetRelationshipCollection();
            if (this.preserveTemplate)
            {
                ((XlsxSheetPreservationData) ws.Tag).DrawingPath = partPath;
            }
            relationshipCollection.LoadRels(partPath, base.builder);
            XmlTextReader reader = new XmlTextReader(base.builder.GetPart(partPath));
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "xdr:twoCellAnchor"))
                    {
                        this.ReadImage(ws, reader, relationshipCollection);
                    }
                }
            }
            finally
            {
                reader.Close();
            }
        }

        private void ReadFills(XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                FillPatternData data = new FillPatternData();
                while (reader.Read())
                {
                    if (reader.NodeType != XmlNodeType.Element)
                    {
                        goto Label_009B;
                    }
                    string name = reader.Name;
                    if (name != null)
                    {
                        if (!(name == "patternFill"))
                        {
                            if (name == "fgColor")
                            {
                                goto Label_0073;
                            }
                            if (name == "bgColor")
                            {
                                goto Label_0087;
                            }
                        }
                        else
                        {
                            string attribute = reader.GetAttribute("patternType");
                            if (attribute != null)
                            {
                                data.PatternStyle = StringToFillPattern(attribute);
                            }
                        }
                    }
                    continue;
                Label_0073:
                    ReadColorElement(reader, ref data.PatternForegroundColor, ref data.PatternForegroundColorIndex);
                    continue;
                Label_0087:
                    ReadColorElement(reader, ref data.PatternBackgroundColor, ref data.PatternBackgroundColorIndex);
                    continue;
                Label_009B:
                    if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "fill")
                        {
                            this.fillPatterns.Add(data);
                            data = new FillPatternData();
                        }
                        if (reader.Name == "fills")
                        {
                            return;
                        }
                    }
                }
            }
        }

        private void ReadFonts(XmlReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                ExcelFontData fontData = new ExcelFontData(null, -1);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "b":
                            {
                                fontData.Weight = 700;
                                continue;
                            }
                            case "i":
                            {
                                fontData.Italic = true;
                                continue;
                            }
                            case "strike":
                            {
                                fontData.Strikeout = true;
                                continue;
                            }
                            case "u":
                            {
                                string attribute = reader.GetAttribute("val");
                                if (attribute == null)
                                {
                                    attribute = "single";
                                }
                                fontData.UnderlineStyle = (UnderlineStyle) Enum.Parse(typeof(UnderlineStyle), attribute, true);
                                continue;
                            }
                            case "vertAlign":
                            {
                                string reqAttrString = Utilities.GetReqAttrString(reader, "val");
                                fontData.ScriptPosition = (ScriptPosition) Enum.Parse(typeof(ScriptPosition), reqAttrString, true);
                                continue;
                            }
                            case "sz":
                            {
                                fontData.Size = (int) (20f * Utilities.GetReqAttrFloat(reader, "val"));
                                continue;
                            }
                            case "color":
                            {
                                ReadColorElement(reader, ref fontData.Color, ref fontData.ColorIndex);
                                continue;
                            }
                            case "name":
                            {
                                fontData.Name = Utilities.GetReqAttrString(reader, "val");
                                continue;
                            }
                            case "font":
                            {
                                if (reader.IsEmptyElement)
                                {
                                    this.AddFontDataCreateNew(ref fontData);
                                }
                                continue;
                            }
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        if (reader.Name == "font")
                        {
                            this.AddFontDataCreateNew(ref fontData);
                            continue;
                        }
                        if (reader.Name == "fonts")
                        {
                            return;
                        }
                    }
                }
            }
        }

        private string[] ReadFromWorkbookXml(ExcelWorksheetCollection sheets, SpreadsheetRelationshipCollection relationshipCollection, out int activeSheet)
        {
            relationshipCollection.GetTypeCount("application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml");
            ArrayList list = new ArrayList();
            int num = 0;
            activeSheet = 0;
            XmlTextReader reader = new XmlTextReader(base.builder.GetPart("/xl/workbook.xml"));
            try
            {
                reader.MoveToContent();
                while (reader.Read())
                {
                    string str4;
                    int num2;
                    string str7;
                    if ((reader.NodeType == XmlNodeType.Element) && ((str7 = reader.Name) != null))
                    {
                        if (!(str7 == "sheet"))
                        {
                            if (str7 == "workbookView")
                            {
                                goto Label_00D4;
                            }
                            if (str7 == "definedName")
                            {
                                goto Label_00FE;
                            }
                        }
                        else
                        {
                            string worksheetName = Utilities.GetReqAttrString(reader, "name");
                            string str2 = Utilities.GetReqAttrString(reader, "r:id");
                            string str3 = "/xl/worksheets/" + Path.GetFileName(relationshipCollection[str2].Target);
                            sheets.Add(worksheetName);
                            list.Add(str3);
                            num++;
                        }
                    }
                    continue;
                Label_00D4:
                    str4 = reader.GetAttribute("activeTab");
                    if ((str4 != null) && (str4 != ""))
                    {
                        activeSheet = int.Parse(str4);
                    }
                    continue;
                Label_00FE:
                    num2 = Utilities.GetOptAttrInt32(reader, "localSheetId", -1);
                    string reqAttrString = Utilities.GetReqAttrString(reader, "name");
                    string rangeString = reader.ReadString();
                    if (rangeString.IndexOf("#REF!") == -1)
                    {
                        this.savedDefinedNames.Add(new SavedDefinedName(num2, reqAttrString, rangeString));
                    }
                }
            }
            finally
            {
                reader.Close();
            }
            return (string[]) list.ToArray(typeof(string));
        }

        private static void ReadHiddenOutlineCollapsed(XmlReader reader, out int outlineLevel, out bool collapsed, out bool hidden)
        {
            outlineLevel = Utilities.GetOptAttrInt32(reader, "outlineLevel", -1);
            collapsed = Utilities.GetOptAttrBool01(reader, "collapsed", false);
            hidden = Utilities.GetOptAttrBool01(reader, "hidden", false);
        }

        private void ReadHyperlinks(ExcelWorksheet ws, XmlReader reader, SpreadsheetRelationshipCollection relationshipCollection)
        {
            while (reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "hyperlinks"))
                {
                    return;
                }
                if ((reader.Name == "hyperlink") && (reader.NodeType == XmlNodeType.Element))
                {
                    int num;
                    int num2;
                    string position = Utilities.GetReqAttrString(reader, "ref").Split(new char[] { ':' })[0];
                    string attribute = reader.GetAttribute("tooltip");
                    string str3 = reader.GetAttribute("location");
                    string str4 = reader.GetAttribute("r:id");
                    SpreadsheetHyperlink hyperlink = ws.Cells[position].Hyperlink;
                    CellRange.PositionToRowColumn(position, out num, out num2);
                    hyperlink.Row = num;
                    hyperlink.Column = num2;
                    if (attribute != null)
                    {
                        hyperlink.ToolTip = attribute;
                    }
                    if (str3 != null)
                    {
                        hyperlink.Location = str3;
                    }
                    if (str4 != null)
                    {
                        hyperlink.Location = relationshipCollection[str4].Target;
                        hyperlink.IsExternal = true;
                    }
                }
            }
        }

        private void ReadImage(ExcelWorksheet ws, XmlTextReader reader, SpreadsheetRelationshipCollection relationshipCollection)
        {
            PositioningMode moveAndSize;
            AnchorCell topLeftCell = null;
            AnchorCell bottomRightCell = null;
            MemoryStream destination = new MemoryStream();
            bool flag = false;
            string path = string.Empty;
            if (reader.IsEmptyElement)
            {
                return;
            }
            string attribute = reader.GetAttribute("editAs");
            if (attribute != null)
            {
                if (!(attribute == "oneCell"))
                {
                    if (attribute == "absolute")
                    {
                        moveAndSize = PositioningMode.FreeFloating;
                        goto Label_0134;
                    }
                }
                else
                {
                    moveAndSize = PositioningMode.Move;
                    goto Label_0134;
                }
            }
            moveAndSize = PositioningMode.MoveAndSize;
        Label_0134:
            while (reader.Read())
            {
                if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "xdr:twoCellAnchor"))
                {
                    break;
                }
                string name = reader.Name;
                if (name != null)
                {
                    if (!(name == "xdr:from"))
                    {
                        if (name == "xdr:to")
                        {
                            goto Label_00D7;
                        }
                        if (name == "xdr:pic")
                        {
                            goto Label_00E7;
                        }
                        if (name == "a:blip")
                        {
                            goto Label_00EC;
                        }
                    }
                    else
                    {
                        topLeftCell = this.ReadAnchorCell(ws, reader, "xdr:from");
                    }
                }
                continue;
            Label_00D7:
                bottomRightCell = this.ReadAnchorCell(ws, reader, "xdr:to");
                continue;
            Label_00E7:
                flag = true;
                continue;
            Label_00EC:
                if (flag)
                {
                    path = "/xl/media/" + Path.GetFileName(relationshipCollection[Utilities.GetReqAttrString(reader, "r:embed")].Target);
                    Utilities.CopyStream(base.builder.GetPart(path), destination);
                    destination.Seek(0L, SeekOrigin.Begin);
                }
            }
            if (flag)
            {
                ws.Pictures.Add(destination, moveAndSize, topLeftCell, bottomRightCell, ExcelPicture.GetExcelPictureFormatFromFileName(path));
            }
        }

        private void ReadLegacyDrawing(ExcelWorksheet ws, SpreadsheetRelationshipCollection relationshipCollection, XmlTextReader reader)
        {
            string legacyDrawingPath = Path.Combine("/xl/drawings/", Path.GetFileName(relationshipCollection[Utilities.GetReqAttrString(reader, "r:id")].Target));
            if (this.preserveTemplate)
            {
                ((XlsxSheetPreservationData) ws.Tag).LegacyDrawingPath = legacyDrawingPath;
            }
            Hashtable commentShapesVisible = this.ReadCommentShapes(ws, legacyDrawingPath);
            this.ReadCommentsIfExists(ws, commentShapesVisible, relationshipCollection.GetFirstVisibleOccurrencePath("http://schemas.openxmlformats.org/officeDocument/2006/relationships/comments"));
        }

        private void ReadMergedRanges(ExcelWorksheet ws, XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "mergeCells"))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "mergeCell"))
                    {
                        string[] strArray = Utilities.GetReqAttrString(reader, "ref").Split(new char[] { ':' }, 2);
                        ws.Cells.GetSubrange(strArray[0], strArray[1]).Merged = true;
                    }
                }
            }
        }

        private void ReadNumberFormats(XmlReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "numFmts"))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "numFmt"))
                    {
                        int index = Utilities.GetReqAttrInt32(reader, "numFmtId");
                        string reqAttrString = Utilities.GetReqAttrString(reader, "formatCode");
                        this.numberFormats.SetNumberFormat(index, reqAttrString);
                    }
                }
            }
        }

        private static void ReadPageBreaks(ExcelWorksheet ws, XmlTextReader reader, string elementName)
        {
            bool flag = elementName == "rowBreaks";
            if (!reader.IsEmptyElement)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == elementName))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "brk"))
                    {
                        int row = Utilities.GetOptAttrInt32(reader, "id", 0);
                        int firstColumn = Utilities.GetOptAttrInt32(reader, "min", 0);
                        int lastColumn = Utilities.GetOptAttrInt32(reader, "max", 0);
                        if ((firstColumn != 0) || (lastColumn != 0))
                        {
                            if (flag)
                            {
                                ws.HorizontalPageBreaks.Add(row, firstColumn, lastColumn);
                            }
                            else
                            {
                                ws.VerticalPageBreaks.Add(row, firstColumn, lastColumn);
                            }
                        }
                        else if (flag)
                        {
                            ws.HorizontalPageBreaks.Add(row);
                        }
                        else
                        {
                            ws.VerticalPageBreaks.Add(row);
                        }
                    }
                }
            }
        }

        private void ReadPageMargins(ExcelWorksheet ws, XmlTextReader reader)
        {
            ws.PrintOptions.TopMargin = Utilities.GetReqAttrFloat(reader, "top");
            ws.PrintOptions.BottomMargin = Utilities.GetReqAttrFloat(reader, "bottom");
            ws.PrintOptions.LeftMargin = Utilities.GetReqAttrFloat(reader, "left");
            ws.PrintOptions.RightMargin = Utilities.GetReqAttrFloat(reader, "right");
            ws.PrintOptions.HeaderMargin = Utilities.GetReqAttrFloat(reader, "header");
            ws.PrintOptions.FooterMargin = Utilities.GetReqAttrFloat(reader, "footer");
        }

        private void ReadPageSetupSettings(ExcelWorksheet ws, XmlTextReader reader)
        {
            ExcelPrintOptions printOptions = ws.PrintOptions;
            printOptions.AutomaticPageBreakScalingFactor = Utilities.GetOptAttrInt32(reader, "scale", 100);
            printOptions.PaperSize = Utilities.GetOptAttrInt32(reader, "paperSize", 1);
            printOptions.StartPageNumber = Utilities.GetOptAttrInt32(reader, "firstPageNumber", 1);
            ws.fitWorksheetWidthToPages = (ushort) Utilities.GetOptAttrInt32(reader, "fitToWidth", 1);
            ws.fitWorksheetHeightToPages = (ushort) Utilities.GetOptAttrInt32(reader, "fitToHeight", 1);
            printOptions.PrintBlackWhite = Utilities.GetOptAttrBool01(reader, "blackAndWhite", false);
            printOptions.DraftQuality = Utilities.GetOptAttrBool01(reader, "draft", false);
            printOptions.UseStartPageNumber = Utilities.GetOptAttrBool01(reader, "useFirstPageNumber", false);
            printOptions.PrintResolution = this.GetPrintResolutionFromString(reader.GetAttribute("horizontalDpi"), 600);
            printOptions.VerticalPrintResolution = this.GetPrintResolutionFromString(reader.GetAttribute("verticalDpi"), 600);
            printOptions.NumberOfCopies = Utilities.GetOptAttrInt32(reader, "copies", 1);
            printOptions.Portrait = GetPortraitValue(reader.GetAttribute("orientation"), true);
            printOptions.PrintCellNotes = GetPrintCellNotesValue(reader.GetAttribute("cellComments"), false);
            printOptions.PrintNotesSheetEnd = GetPrintNotesSheetEndValue(reader.GetAttribute("cellComments"), false);
        }

        private void ReadSharedStrings()
        {
            if (base.builder.Parts.Contains("/xl/sharedStrings.xml"))
            {
                XmlTextReader reader = new XmlTextReader(base.builder.GetPart("/xl/sharedStrings.xml"));
                try
                {
                    string str = null;
                    while (reader.Read())
                    {
                        if (reader.NodeType != XmlNodeType.Element)
                        {
                            goto Label_009C;
                        }
                        string name = reader.Name;
                        if (name != null)
                        {
                            if (!(name == "si"))
                            {
                                if (name == "t")
                                {
                                    goto Label_0081;
                                }
                            }
                            else if (reader.IsEmptyElement)
                            {
                                this.strings.Add("");
                            }
                        }
                        continue;
                    Label_0081:
                        if (str == null)
                        {
                            str = reader.ReadString();
                        }
                        else
                        {
                            str = str + reader.ReadString();
                        }
                        continue;
                    Label_009C:
                        if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "si"))
                        {
                            this.strings.Add(str);
                            str = null;
                        }
                    }
                }
                finally
                {
                    reader.Close();
                }
            }
        }

        private void ReadSheetData(ExcelWorksheet ws, XmlTextReader reader)
        {
            ExcelCell cell = null;
            string str = null;
            string s = null;
            CellStyle style = null;
            object obj2;
            bool flag;
            string str8;
            if (reader.IsEmptyElement)
            {
                return;
            }
        Label_0011:
            flag = true;
            if ((reader.NodeType == XmlNodeType.Element) && ((str8 = reader.Name) != null))
            {
                if (!(str8 == "row"))
                {
                    if (str8 == "c")
                    {
                        style = null;
                        string attribute = reader.GetAttribute("r");
                        if (attribute != null)
                        {
                            int num4;
                            int num5;
                            CellRange.PositionToRowColumn(attribute, out num4, out num5);
                            cell = ws.Rows[num4].AllocatedCells[num5];
                            str = reader.GetAttribute("t");
                            s = reader.GetAttribute("s");
                            if (s != null)
                            {
                                int num6 = int.Parse(s);
                                style = (CellStyle) this.cellStyles[num6];
                                cell.Style = style;
                            }
                        }
                    }
                    else
                    {
                        string str3;
                        if (str8 == "f")
                        {
                            string str6 = null;
                            string str7 = reader.GetAttribute("t");
                            if (str7 == "shared")
                            {
                                str6 = reader.GetAttribute("ref");
                            }
                            str3 = "=" + reader.ReadElementString();
                            flag = false;
                            if (str7 == "shared")
                            {
                                if (str6 != null)
                                {
                                    string[] strArray = str6.Split(new char[] { ':' }, 2);
                                    ws.Cells.GetSubrange(strArray[0], strArray[1]).Formula = str3;
                                }
                            }
                            else
                            {
                                cell.Formula = str3;
                            }
                        }
                        else
                        {
                            if (str8 == "v")
                            {
                                int num;
                                str3 = reader.ReadElementString();
                                flag = false;
                                if (str == null)
                                {
                                    str = "n";
                                }
                                switch (str)
                                {
                                    case "s":
                                        num = int.Parse(str3);
                                        obj2 = this.strings[num];
                                        goto Label_033F;

                                    case "b":
                                        num = int.Parse(str3);
                                        if (num != 1)
                                        {
                                            if (num != 0)
                                            {
                                                throw new SpreadsheetException("Error: boolean value not in correct format (0/1).");
                                            }
                                            obj2 = false;
                                        }
                                        else
                                        {
                                            obj2 = true;
                                        }
                                        goto Label_033F;

                                    case "n":
                                        obj2 = Utilities.TryCastingToInt(double.Parse(str3, NumberStyles.Float, CultureInfo.InvariantCulture));
                                        goto Label_033F;

                                    case "e":
                                        obj2 = str3;
                                        goto Label_033F;

                                    case "str":
                                        obj2 = str3;
                                        goto Label_033F;

                                    case "inlineStr":
                                        obj2 = null;
                                        goto Label_033F;
                                }
                                obj2 = null;
                                goto Label_033F;
                            }
                            if (str8 == "t")
                            {
                                cell.Value = reader.ReadElementString();
                                flag = false;
                            }
                        }
                    }
                }
                else
                {
                    string str4 = reader.GetAttribute("r");
                    if (str4 != null)
                    {
                        int num3;
                        bool flag3;
                        bool flag4;
                        ExcelRow excelColumnRow = ws.Rows[str4];
                        float num2 = Utilities.GetOptAttrFloat(reader, "ht", 0f);
                        if (Utilities.GetOptAttrBool01(reader, "customHeight", false))
                        {
                            excelColumnRow.Height = (int) (num2 * 20f);
                        }
                        ReadHiddenOutlineCollapsed(reader, out num3, out flag3, out flag4);
                        SetOutlineCollapsedHiddenIfNeeded(excelColumnRow, num3, flag3, flag4);
                    }
                }
            }
            goto Label_03DA;
        Label_033F:
            if ((style != null) && ((obj2 is double) || (obj2 is int)))
            {
                int numberFormatIndex = style.Element.Indexes.NumberFormatIndex;
                if (((numberFormatIndex >= 14) && (numberFormatIndex <= 0x16)) || XLSFileReader.IsDateTime(style.Element.NumberFormat))
                {
                    if (obj2 is int)
                    {
                        obj2 = (int) obj2;
                    }
                    obj2 = ExcelCell.ConvertExcelNumberToDateTime((double) obj2, base.excelFile.Use1904DateSystem);
                }
            }
            cell.Value = obj2;
            object obj1 = cell.Value;
        Label_03DA:
            if (flag)
            {
                reader.Read();
            }
            if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "sheetData"))
            {
                return;
            }
            goto Label_0011;
        }

        private void ReadSheetProperties(ExcelWorksheet ws, XmlTextReader reader)
        {
            if (!reader.IsEmptyElement)
            {
                while (reader.Read())
                {
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "sheetPr"))
                    {
                        return;
                    }
                    if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "pageSetUpPr"))
                    {
                        ws.PrintOptions.FitToPage = Utilities.GetOptAttrBool01(reader, "fitToPage", false);
                    }
                    else if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "outlinePr"))
                    {
                        ws.ViewOptions.OutlineRowButtonsBelow = Utilities.GetOptAttrBool01(reader, "summaryBelow", ws.ViewOptions.OutlineRowButtonsBelow);
                        ws.ViewOptions.OutlineColumnButtonsRight = Utilities.GetOptAttrBool01(reader, "summaryRight", ws.ViewOptions.OutlineColumnButtonsRight);
                    }
                }
            }
        }

        private void ReadStyles()
        {
            Stream part = base.builder.GetPart("/xl/styles.xml");
            XLSFileReader.LoadDefaultPalette(ref this.colors);
            XmlTextReader reader = new XmlTextReader(part);
            try
            {
                reader.MoveToContent();
                reader.Read();
                while (!reader.EOF)
                {
                    bool flag = true;
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "numFmts":
                                this.ReadNumberFormats(reader);
                                goto Label_0159;

                            case "fonts":
                                this.ReadFonts(reader);
                                goto Label_0159;

                            case "fills":
                                this.ReadFills(reader);
                                goto Label_0159;

                            case "borders":
                                this.ReadBorders(reader);
                                goto Label_0159;

                            case "cellXfs":
                                this.ReadCellXfs(reader);
                                goto Label_0159;

                            case "colors":
                                this.ReadColors(reader);
                                goto Label_0159;

                            case "dxfs":
                                if (this.preserveTemplate)
                                {
                                    ((XlsxExcelFilePreservationData) base.excelFile.Tag).DxfsStyleOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0159;
                        }
                        reader.Skip();
                        flag = false;
                    }
                Label_0159:
                    if (flag)
                    {
                        reader.Read();
                    }
                }
            }
            finally
            {
                reader.Close();
            }
            this.ConvertIndexes();
        }

        private void ReadViewOptions(ExcelWorksheet ws, XmlTextReader reader)
        {
            bool flag = false;
            if (!reader.IsEmptyElement)
            {
                while (!reader.EOF)
                {
                    bool flag2 = true;
                    if ((reader.NodeType == XmlNodeType.EndElement) && (reader.Name == "sheetViews"))
                    {
                        return;
                    }
                    if (((reader.NodeType == XmlNodeType.Element) && (reader.Name == "sheetView")) && (Utilities.GetReqAttrInt32(reader, "workbookViewId") == 0))
                    {
                        this.GetFirstVisibleCellIndexes(ws, reader);
                        ws.ViewOptions.Zoom = Utilities.GetOptAttrInt32(reader, "zoomScaleNormal", 100);
                        ws.ViewOptions.PageBreakViewZoom = Utilities.GetOptAttrInt32(reader, "zoomScaleSheetLayoutView", 100);
                        ws.ViewOptions.ShowFormulas = Utilities.GetOptAttrBool01(reader, "showFormulas", false);
                        ws.ViewOptions.ShowGridLines = Utilities.GetOptAttrBool01(reader, "showGridLines", true);
                        ws.ViewOptions.ShowSheetHeaders = Utilities.GetOptAttrBool01(reader, "showRowColHeaders", true);
                        ws.ViewOptions.ShowZeroValues = Utilities.GetOptAttrBool01(reader, "showZeros", true);
                        ws.ViewOptions.ShowColumnsFromRightToLeft = Utilities.GetOptAttrBool01(reader, "rightToLeft", false);
                        ws.ViewOptions.ShowOutlineSymbols = Utilities.GetOptAttrBool01(reader, "showOutlineSymbols", true);
                        ws.ViewOptions.ShowInPageBreakPreview = GetShowInPageBreakPreviewValue(reader.GetAttribute("view"), false);
                        flag = true;
                    }
                    else if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "sheetView"))
                    {
                        flag = false;
                    }
                    else if (((reader.NodeType == XmlNodeType.Element) && (reader.Name == "pane")) && flag)
                    {
                        ((XlsxSheetPreservationData) ws.Tag).PaneOuterString = reader.ReadOuterXml();
                        flag2 = false;
                    }
                    if (flag2)
                    {
                        reader.Read();
                    }
                }
            }
        }

        private void ReadWorksheet(ExcelWorksheet ws, string sheetPath)
        {
            SpreadsheetRelationshipCollection relationshipCollection = new SpreadsheetRelationshipCollection();
            XlsxSheetPreservationData data = new XlsxSheetPreservationData();
            relationshipCollection.LoadRels(sheetPath, base.builder);
            if (this.preserveTemplate)
            {
                data.SourceSheetPath = sheetPath;
                ws.Tag = data;
            }
            XmlTextReader reader = new XmlTextReader(base.builder.GetPart(sheetPath));
            try
            {
                reader.MoveToContent();
                reader.Read();
                while (!reader.EOF)
                {
                    bool flag = true;
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        switch (reader.Name)
                        {
                            case "sheetPr":
                                this.ReadSheetProperties(ws, reader);
                                goto Label_0386;

                            case "cols":
                                this.ReadColumnInfos(ws, reader);
                                goto Label_0386;

                            case "sheetData":
                                this.ReadSheetData(ws, reader);
                                goto Label_0386;

                            case "mergeCells":
                                this.ReadMergedRanges(ws, reader);
                                goto Label_0386;

                            case "pageSetup":
                                this.ReadPageSetupSettings(ws, reader);
                                goto Label_0386;

                            case "pageMargins":
                                this.ReadPageMargins(ws, reader);
                                goto Label_0386;

                            case "sheetViews":
                                this.ReadViewOptions(ws, reader);
                                goto Label_0386;

                            case "drawing":
                                this.ReadDrawings(ws, relationshipCollection[Utilities.GetReqAttrString(reader, "r:id")].Target);
                                goto Label_0386;

                            case "legacyDrawing":
                                this.ReadLegacyDrawing(ws, relationshipCollection, reader);
                                goto Label_0386;

                            case "legacyDrawingHF":
                                if (this.preserveTemplate)
                                {
                                    data.LegacyDrawingHFPath = Path.Combine("/xl/drawings/", Path.GetFileName(relationshipCollection[Utilities.GetReqAttrString(reader, "r:id")].Target));
                                }
                                goto Label_0386;

                            case "sheetFormatPr":
                                this.ReadDefaultColumnWidth(ws, reader);
                                goto Label_0386;

                            case "sheetProtection":
                                if (Utilities.GetOptAttrBool01(reader, "sheet", false))
                                {
                                    ws.Protected = true;
                                }
                                if (this.preserveTemplate)
                                {
                                    data.SheetProtectionOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0386;

                            case "autoFilter":
                                if (this.preserveTemplate)
                                {
                                    data.AutoFilterOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0386;

                            case "conditionalFormatting":
                                if (this.preserveTemplate)
                                {
                                    data.ConditionalFormattingOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0386;

                            case "hyperlinks":
                                this.ReadHyperlinks(ws, reader, relationshipCollection);
                                goto Label_0386;

                            case "headerFooter":
                                if (this.preserveTemplate)
                                {
                                    data.HeaderFooterOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0386;

                            case "dataValidations":
                                if (this.preserveTemplate)
                                {
                                    data.DataValidationOuterString = reader.ReadOuterXml();
                                    flag = false;
                                }
                                goto Label_0386;

                            case "rowBreaks":
                                ReadPageBreaks(ws, reader, "rowBreaks");
                                goto Label_0386;

                            case "colBreaks":
                                ReadPageBreaks(ws, reader, "colBreaks");
                                goto Label_0386;
                        }
                        reader.Skip();
                        flag = false;
                    }
                Label_0386:
                    if (flag)
                    {
                        reader.Read();
                    }
                }
            }
            finally
            {
                reader.Close();
            }
        }

        private void ReadWorksheets()
        {
            int num;
            ExcelWorksheetCollection sheets = base.excelFile.Worksheets;
            SpreadsheetRelationshipCollection relationshipCollection = new SpreadsheetRelationshipCollection();
            relationshipCollection.LoadRels("/xl/workbook.xml", base.builder);
            string[] strArray = this.ReadFromWorkbookXml(sheets, relationshipCollection, out num);
            for (int i = 0; i < strArray.Length; i++)
            {
                this.ReadWorksheet(sheets[i], strArray[i]);
            }
            foreach (SavedDefinedName name in this.savedDefinedNames)
            {
                ExcelWorksheet worksheet;
                if (name.globalName)
                {
                    worksheet = sheets[name.parentSheet];
                }
                else
                {
                    worksheet = sheets[name.localSheetId];
                }
                worksheet.NamedRanges.Add(null, name.name, sheets[name.parentSheet].Cells.GetSubrange(name.firstCell, name.lastCell), name.globalName);
            }
            sheets.ActiveWorksheet = sheets[num];
        }

        private void ReadXfAlignment(XmlTextReader reader, CellStyleData csData)
        {
            string attribute = reader.GetAttribute("horizontal");
            if ((attribute != null) && (attribute != ""))
            {
                if (attribute == "centerContinuous")
                {
                    attribute = "centerAcross";
                }
                csData.HorizontalAlignment = (HorizontalAlignmentStyle) Enum.Parse(typeof(HorizontalAlignmentStyle), attribute, true);
            }
            string str2 = reader.GetAttribute("vertical");
            if ((str2 != null) && (str2 != ""))
            {
                csData.VerticalAlignment = (VerticalAlignmentStyle) Enum.Parse(typeof(VerticalAlignmentStyle), str2, true);
            }
            string s = reader.GetAttribute("indent");
            if ((s != null) && (s != ""))
            {
                csData.Indent = int.Parse(s);
            }
            string str4 = reader.GetAttribute("textRotation");
            if ((str4 != null) && (str4 != ""))
            {
                int num = int.Parse(str4);
                if ((num > 90) && (num < 0xff))
                {
                    csData.Rotation = num - 0x100;
                }
                else
                {
                    csData.Rotation = num;
                }
            }
            if (reader.GetAttribute("wrapText") == "1")
            {
                csData.WrapText = true;
            }
            if (reader.GetAttribute("shrinkToFit") == "1")
            {
                csData.ShrinkToFit = true;
            }
        }

        private CellStyle ReadXfElement(XmlTextReader reader, out bool applyAlignment)
        {
            CellStyle style = new CellStyle(base.excelFile);
            CellStyleData element = style.Element;
            element.Indexes = new CellStyleDataIndexes();
            string attribute = reader.GetAttribute("numFmtId");
            string s = reader.GetAttribute("fontId");
            string str3 = reader.GetAttribute("fillId");
            string str4 = reader.GetAttribute("borderId");
            string str5 = reader.GetAttribute("applyNumberFormat");
            reader.GetAttribute("applyFont");
            string str6 = reader.GetAttribute("applyFill");
            string str7 = reader.GetAttribute("applyBorder");
            if ((str5 == "1") && (attribute != null))
            {
                element.Indexes.NumberFormatIndex = int.Parse(attribute);
            }
            if (s != null)
            {
                element.Indexes.FontIndex = int.Parse(s);
            }
            if ((str6 == "1") && (str3 != null))
            {
                element.Indexes.FillPatternIndex = int.Parse(str3);
            }
            if ((str7 == "1") && (str4 != null))
            {
                element.Indexes.BordersIndex = int.Parse(str4);
            }
            applyAlignment = Utilities.GetOptAttrBool01(reader, "applyAlignment", false);
            return style;
        }

        private static Color RgbStringToColor(string rgbStr)
        {
            return Color.FromArgb(int.Parse(rgbStr, NumberStyles.AllowHexSpecifier));
        }

        private static void SetOutlineCollapsedHiddenIfNeeded(ExcelColumnRowBase excelColumnRow, int outlineLevel, bool collapsed, bool hidden)
        {
            if (outlineLevel != -1)
            {
                excelColumnRow.OutlineLevel = outlineLevel;
            }
            if (collapsed)
            {
                excelColumnRow.Collapsed = true;
            }
            if (hidden)
            {
                excelColumnRow.Hidden = true;
            }
        }

        private static FillPatternStyle StringToFillPattern(string patternTypeStr)
        {
            int num = (int) Enum.Parse(typeof(XlsxFillStyle), patternTypeStr, true);
            return (FillPatternStyle) num;
        }

        private class SavedDefinedName
        {
            public string firstCell;
            public bool globalName;
            public string lastCell;
            public int localSheetId;
            public string name;
            public string parentSheet;

            public SavedDefinedName(int localSheetId, string name, string rangeString)
            {
                this.ParseRangeString(this.GetFirstRangeString(rangeString));
                this.GetCellString(ref this.firstCell, 1);
                this.GetCellString(ref this.lastCell, 0x100000);
                this.localSheetId = localSheetId;
                if (localSheetId == -1)
                {
                    this.globalName = true;
                }
                else
                {
                    this.globalName = false;
                }
                this.name = name;
            }

            private void GetCellString(ref string cellStr, int defaultRow)
            {
                if (!Utilities.IsStringNumeric(cellStr.Substring(cellStr.Length - 1, 1)))
                {
                    cellStr = cellStr + defaultRow.ToString();
                }
            }

            private string GetFirstRangeString(string rangeString)
            {
                string str;
                int length = -1;
                do
                {
                    length = rangeString.IndexOf(",", (int) (length + 1));
                    if (length == -1)
                    {
                        return rangeString;
                    }
                    str = rangeString.Substring(0, length);
                }
                while ((Utilities.CountCharOccurenceInString(str, '\'') % 2) != 0);
                return str;
            }

            private void ParseRangeString(string rangeString)
            {
                string[] strArray = new string[2];
                int length = rangeString.LastIndexOf('!');
                if (rangeString.StartsWith("'"))
                {
                    this.parentSheet = rangeString.Substring(1, length - 2);
                }
                else
                {
                    this.parentSheet = rangeString.Substring(0, length);
                }
                this.parentSheet = this.parentSheet.Replace("''", "'");
                strArray = rangeString.Substring(length + 1).Split(new char[] { ':' });
                this.firstCell = strArray[0].Replace("$", "");
                if (strArray.Length > 1)
                {
                    this.lastCell = strArray[1].Replace("$", "");
                }
                else
                {
                    this.lastCell = this.firstCell;
                }
            }
        }
    }
}
